package com.mytx.notify.config;

import net.openmob.mobileimsdk.server.ServerLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Configuration
public class ImStart implements CommandLineRunner {

    @Autowired
    ServerLauncher serverLauncher;

    @Override
    public void run(String... args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    serverLauncher.startup();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // 加一个钩子，确保在JVM退出时释放netty的资源
                Runtime.getRuntime().addShutdownHook(new Thread() {
                    @Override
                    public void run() {
                        serverLauncher.shutdown();
                    }
                });

            }
        }).start();
    }
}
